<?php
/**
*
* @package phpBB3
* @version $Id$
* @copyright (c) 2010
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/	
	
 
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include_once($phpbb_root_path . 'common.' . $phpEx);
include_once($phpbb_root_path . 'sv_common.' . $phpEx);
include_once($phpbb_root_path . 'prophet_common.' . $phpEx);
include_once($phpbb_root_path . 'ANN2/class_neuralnetwork.' . $phpEx);



/*		
$year_set = array(
		'2012',
		
		);	
$month_set = array(
		'01', '02', 
		);
$day_set = array(
		'01', '02', '03', '04', '05',		
		);
*/		
		

		
		
//$debug = true;
//$debug = false;

$corps_in_local_reports = array();

$gen_local_report_for_corps_to_watch		= request_var('gen_local_report_for_corps_to_watch', '');
if ($gen_local_report_for_corps_to_watch == 1)
{
	// output some message since we are not outputing a csv file
	//$debug = true;
	
	gen_local_report_for_corps_to_watch();
	
	return;
}

$gen_local_report_for_all_big_corps		= request_var('gen_local_report_for_all_big_corps', '');
if ($gen_local_report_for_all_big_corps == 1)
{
	// output some message since we are not outputing a csv file
	//$debug = true;
	
	gen_local_report_for_all_big_corps();
	
	return;
}

$gen_local_report_for_all_corps		= request_var('gen_local_report_for_all_corps', '');
if ($gen_local_report_for_all_corps == 1)
{
	// output some message since we are not outputing a csv file
	//$debug = true;
	
	gen_local_report_for_all_corps();
	
	return;
}


$today = get_date_from_time(get_taiwan_time());	


$get_all_data_for_corp_to_watch		= request_var('get_all_data_for_corp_to_watch', '');
if ($get_all_data_for_corp_to_watch == 1)
{
	// output some message since we are not outputing a csv file
	$debug = true;
	
	$s = time();	
	
	// update TAIEX first 
	get_TWSE_data();
	
	get_stock_data(get_corp_to_watch());
	
	$e = time() - $s;
	echo "Done - get_all_data_for_corp_to_watch, $e seconds";
	
	return;
}


$get_all_big_stock_data		= request_var('get_all_big_stock_data', '');
if ($get_all_big_stock_data == 1)
{
	// output some message since we are not outputing a csv file
	$debug = true;
	
	$s = time();	
	
	// update TAIEX first 
	get_TWSE_data();
	
	get_stock_data($big_corp);
	
	$e = time() - $s;
	echo "Done - get_all_big_stock_data, $e seconds";
	
	return;
}

$get_all_stock_data		= request_var('get_all_stock_data', '');
if ($get_all_stock_data == 1)
{
	global $all_corp;
	
	// output some message since we are not outputing a csv file
	$debug = true;
	
	$s = time();	
	
	// update TAIEX first 
	get_TWSE_data();
	
	$all_corp_id = get_all_corp_id();
	
	get_stock_data($all_corp_id);
	
	$e = time() - $s;
	echo "Done - get_all_stock_data, $e seconds";
	
	return;
}



$get_stock_data		= request_var('get_stock_data', '');
if ($get_stock_data == 1)
{
	$corp_id						= request_var('corp_id', '');
	$update_from_the_beginning		= request_var('update_from_the_beginning', '');
	
	$set = array();
	$set[] = $corp_id;
	
	if ($update_from_the_beginning == 1)
		get_stock_data($set, false);
	else
		get_stock_data($set);
}



$gen_corp_report		= request_var('gen_corp_report', '');
if ($gen_corp_report == 1)
{
	$corp_id		= request_var('corp_id', '');
	
	// output HTTP header
	header("Content-type: application/csv");
	header("Content-Disposition: attachment; filename=stock-$today.csv");
	header("Pragma: no-cache");
	header("Expires: 0");

	gen_report($corp_id);
}

$ann		= request_var('ANN', '');
if ($ann == 1)
{
	$corp_id		= request_var('corp_id', '');
	
	// output HTTP header
	header("Content-type: application/csv");
	header("Content-Disposition: attachment; filename=stock-$today.csv");
	header("Pragma: no-cache");
	header("Expires: 0");

	gen_ann_report($corp_id);
}

function gen_local_report_for_corps_to_watch()
{	
	gen_local_report(get_corp_to_watch());
}

function gen_local_report_for_all_big_corps()
{	
	// TEST!@!!!!!!!!!!
//	$tmp = array('5203', '1101');
//	gen_local_report($tmp);			
//	return;
	
	global $big_corp;
	
	gen_local_report($big_corp);		
}

function gen_local_report_for_all_corps()
{
	$all_corp_id = get_all_corp_id();

	gen_local_report($all_corp_id);	
}

function gen_local_report($corp_id_set)
{
	global $local_report_path;
		
	// make local path
	$date_str 			= date('Y.m.d_H_i', time());
	$local_report_path 	= $local_report_path . $date_str . '\\';
	
	if ($debug)
		echo "local_report_path:$local_report_path <BR>\n";
	
	if (file_exists($local_report_path) == false)
	{
		if (mkdir($local_report_path) == false)
		{
			echo "fail to create dir:$local_report_path <BR>\n";
			return;
		}
	}
		
	// gene report for each corp 
	foreach($corp_id_set as $corp_id)
	{
		gen_report($corp_id, true);				
	}
	
	// generate SPSS syntax file for analysis
	$syntax_file = gen_SPSS_syntax($local_report_path);
	
	// show result path 
	$result_path = $local_report_path . 'Result\\';
	echo "Result path: $result_path <BR><BR>\n";

	echo "STEP 3 : Run SPSS to Analyse <BR>" . PHP_EOL;
	echo "SPSS Syntax File   $syntax_file <BR>\n";	
	echo "	<BR>	<a href='file://$syntax_file' target='_blank' > Open SPSS Syntax File </a> 	<BR>	";
	
	
	// show the link to filer result 	
	//echo "Result path: $result_path <BR>\n";
	echo "	<BR>
	<a href='prophet_analysis.php?analyse_local_report=1&report_path=$result_path' target='_blank' > STEP 4 : Filter and Show Analysis </a> 
	";
	
	
}

function get_stock_data($corp_id_set, $only_add_new_record = true)
{
	global $db, $debug;
	global $year_set, $month_set, $day_set;

	if ($debug)
		echo "get_stock_data - begin <BR>\n";
	
	// don't go over today.
	$today = get_date_from_time(get_taiwan_time());	
	if ($debug)
		echo "today: $today <BR>\n";
	
	foreach ($corp_id_set as $corp_id)
	{
		if ($debug)
		{
			$start_time = date("H:i", get_taiwan_time());
			echo "corp_id: $corp_id<BR>" . PHP_EOL;
			echo "    $start_time<BR>" . PHP_EOL;
		}
						
		$skip_to_next_corp = false;
		
		$last_date	= '';
//		$query 		= 'SELECT * from twse_daily_trading_info WHERE corp_id="'. $corp_id . '" ORDER BY date DESC';
		$query 		= 'SELECT MAX(date) as date from twse_daily_trading_info WHERE corp_id="'. $corp_id . '" ';
		$result		= $db->sql_query_limit($query, 1);
		if ($row = $db->sql_fetchrow($result))
		{
			$last_date = $row['date'];
			if ($debug)
					echo "last_date: $last_date<BR>\n";
					
			// if we never download data for this corp, check if this corp is still tradable in the market 
			if ($last_date == '')
			{
				if ( get_stock_price_volume($corp_id, &$price, &$volume, &$current_short_term_volume, &$reach_surged_limit, &$reach_decline_limit) == false)
				{
					if ($debug)
						echo "No market info for $corp_id, continue to next corp <BR><BR>" . PHP_EOL;
					
					$skip_to_next_corp = true;
				}
				
				// we cannot get the info of the corps listed on OTC from TWSE, so skip them. 
				$y = date("Y", get_taiwan_time());
				$m = date("m", get_taiwan_time());
				$d = date("d", get_taiwan_time());
				
				$url = "http://www.twse.com.tw/ch/trading/exchange/STOCK_DAY/STOCK_DAY_print.php?genpage=genpage/Report$y$m/$y$m" . "_F3_1_8_$corp_id.php&type=csv";
	
    			$raw_file = file_get_contents($url);
    			if ($raw_file == false)
    			{
	    			if ($debug)
						echo "No history info for $corp_id in the latest month. Maybe it's on OTC. continue to next corp <BR><BR>" . PHP_EOL;
	    
					$skip_to_next_corp = true;
				}
			}
		}
		$db->sql_freeresult($result);
		
		if ($skip_to_next_corp)
			continue;
		
			
		foreach ($year_set as $y)
		{
			foreach ($month_set as $m)
			{
				foreach ($day_set as $d)
				{	
					$date = $y . $m . $d;				
					
					if ($only_add_new_record)
					{
						// skip old records.
						if (strcmp($date, $last_date) <=0 )
							continue;						
					}
					
					// don't go over today.
					if (strcmp($date, $today) >0 )
							break;						
	
					$r = get_stock_daily_trading_data($corp_id, $y, $m, $d);				
					if ($r == false)
					{
						if ($debug)
						{
							echo "no data for this month, go on to next month <BR>\n";
							
							if (strcmp($date, $today) ==0 )
								echo "<BR><BR><BR><BR> corp $corp_id, no data for today <BR><BR><BR><BR>". PHP_EOL;
						}
							
						break;	// no data for this month, go on to next month
					}					
				}
			}					
		}
		
		
		
	} //foreach ($corp_id_set as $corp_id)
	
	return;
}

// Get the daily stock trading data of a specific date. 
function get_stock_daily_trading_data($corp_id, $y, $m, $d)
{
	global $db, $debug;
	
	$get_valid_data = false;
	$date = $y . $m . $d;			
	
	// search from database.
	$query	= 'select * from twse_daily_trading_info WHERE corp_id="'. $corp_id . '" AND date="' . $date . '"'; 
	$result	= $db->sql_query_limit($query, 1); 
	if( ($row = $db->sql_fetchrow($result)))
	{
		if ($debug)
			echo "index at $date alrady exist\n";
		return ;
	}	
	$db->sql_freeresult($result);
	
	$url = "http://www.twse.com.tw/ch/trading/exchange/STOCK_DAY/STOCK_DAY_print.php?genpage=genpage/Report$y$m/$y$m" . "_F3_1_8_$corp_id.php&type=csv";
	if ($debug)
		echo $url . "<BR>\n";
	
    $raw_file = file_get_contents($url);
    if ($raw_file == false)
    	return false;
	
    $csv 		= sv_str_getcsv ($raw_file, "\n");    
    //print_r($csv);
    
    
    foreach($csv as &$row)
    {
	    $row = sv_str_getcsv($row[0]);
	    if (sizeof($row[0]) != 9)
	    	continue;
    
//	    print_r($row[0]);
	    	    	     
	    $q_date 			= $row[0][0];
	    $q_trade_volume 	= $row[0][1];
	    $q_trade_value 		= $row[0][2];
	    $q_opening_price 	= (float)($row[0][3]);
	    $q_highest_price 	= (float)($row[0][4]);
	    $q_lowest_price 	= (float)($row[0][5]);
	    $q_closing_price 	= (float)($row[0][6]);
	    $q_change 			= (float)($row[0][7]);
	    $q_transaction 		= $row[0][8];
	    
	    if (strlen($q_date) < 8)
	    	continue;

	    $get_valid_data = true;
	    echo "data validated: $q_date, $q_volume, $q_index\n";	    	       
	    
		$y = substr($q_date, 0, strlen($q_date) -6);
		$m = substr($q_date, strlen($q_date) - 5, 2);
		$d = substr($q_date, strlen($q_date) - 2, 2);
    		
    	$y = (int)($y) + 1911;
    	$q_date = '' . $y . $m . $d;
    		
    	$query	= 'select * from twse_daily_trading_info WHERE corp_id="'. $corp_id . '" AND date="' . $q_date . '"'; 
    	if ($debug)
    	    echo "    $query <BR>\n";
		$result	= $db->sql_query_limit($query, 1); 
		if( ($row = $db->sql_fetchrow($result)))
			continue;
		
    	$q_trade_volume 	= (int)(ereg_replace("[^0-9.]", "", $q_trade_volume));
    	$q_trade_value 		= (int)(ereg_replace("[^0-9.]", "", $q_trade_value));
    	$q_transaction 		= (int)(ereg_replace("[^0-9.]", "", $q_transaction));	
    	    	
    	// insert the index record into the database.
    	$sql = 'INSERT IGNORE INTO twse_daily_trading_info SET corp_id="' . $corp_id . '", date="' . $q_date . '", 
    				trade_volume=' . $q_trade_volume . ', trade_value=' . $q_trade_value . ', 
    				opening_price=' . $q_opening_price . ', highest_price=' . $q_highest_price . ', 
    				lowest_price=' . $q_lowest_price . ', closing_price=' . $q_closing_price . ', 
    				`change`=' . $q_change . ', transaction=' . $q_transaction . ' ';
    				
		if ($debug)
    	    echo "    $sql <BR>\n";
    	$result	= $db->sql_query($sql);   
    	$db->sql_freeresult($result); 		
	}
	
    return $get_valid_data;		
}

function gen_peak($closing_price, $window, $step, $fuzzy, $tolerance)
{	
	// identify peaks and valleys. 
	$i 		= 0;
	$peak	= array();
	
	// set all default values.
	while( $i < sizeof($closing_price))
	{
		$peak[]		= 0;
		++$i;
	}
	
	$i = 0;	
	
	while( $i + $window < sizeof($closing_price))
	{
		// get the max value within the window, set it as a peak.
		$j = get_max($closing_price, $i, $window);
		$peak[$j] = 1;

		// get the min value within the window, set it as a valley.
		$j = get_min($closing_price, $i, $window);
		$peak[$j] = -1;
		
		// move the window forward by step.
		$i = $i + $step;
	}
	
	// make fuzzy backward	
	$i 		= $fuzzy;
	while( $i < sizeof($closing_price))
	{
		// for each peak or valley, 
		if ($peak[$i] == 1 || $peak[$i] == -1)
		{
			$j = 0;
			
			// check if the values behind it close enough. If yes, also set it as a peak or valley. 
			// the rang is limited by fuzzy. 
			while($j < $fuzzy)
			{
				if (abs($closing_price[$i - $j] - $closing_price[$i]) < $tolerance * $closing_price[$i])
				{
					$peak[$i - $j] = $peak[$i];
				}
				else
					break;
				
				++$j;
			}
		}
		
		++$i;
	}
	
	// make fuzzy forward	
	$i 		= 0;
	while( $i + $fuzzy < sizeof($closing_price))
	{
		if ($peak[$i] == 1 || $peak[$i] == -1)
		{
			$j = 0;
			
			while($j < $fuzzy)
			{
				if (abs($closing_price[$i + $j] - $closing_price[$i]) < $tolerance * $closing_price[$i])
				{
					$peak[$i + $j] = $peak[$i];
				}
				else
					break;					
				
				++$j;
			}
		}
		
		$i += $fuzzy;
	}	
	
	return $peak;
}

function gen_report_obselete($corp_id, $local_report = false)
{
	global $db, $debug;
	global $year_set, $month_set, $day_set;
	global $local_report_path, $corps_in_local_reports;
	
	$s = time();
	
	$report	= false;
	
	$str = ' ' . $corp_id . '-' . get_corp_chinese_name($corp_id) . '-date, trade_volume, trade_value, opening_price, highest_price, lowest_price, closing_price, change, transaction, TAIEX, TAIEX_volume, ma5, ma10, ma20, ma30, ma60, ma120, RSI20, RSI40, peak_1_A, peak_1_B, peak_1_C, peak_1_D, peak_1_E, peak_1_F' . "\n";	
	
	if ($local_report)
	{
		// check if the corp has any record and if the corp has enough data to analyse, or SPSS will fail
		// a basic threshold is 5 years => 5*250 = 750 trading days 		
		$query	= 'SELECT COUNT(DISTINCT date) AS num FROM twse_daily_trading_info WHERE corp_id="'. $corp_id . '" '; 
		$result	= $db->sql_query_limit($query, 1); 
		$row 	= $db->sql_fetchrow($result);
		$num	= $row['num'];
		
		if ($row == false || $num < 750)
		{					
			if ($debug)
				echo "no record for $corp_id, or too few records for the corp:$num <BR>\n";							
			return;
		}	
		$db->sql_freeresult($result);
		
		
		// open file to write 
		$file_path 	= $local_report_path . $corp_id . '.csv';
		$report 	= fopen($file_path, 'w');
		if ($report == false)
			return;
			
		fwrite($report, $str);		
	}
	else
	{
		// print csv header.
		echo $str;
	}
	
	// Get all closing_price before calculating peaks and valleys  
	$closing_price		= array();
	
	foreach ($year_set as $y)
	{
		foreach ($month_set as $m)
		{
			foreach ($day_set as $d)
			{
				// get stock price
				$date = $y . $m . $d;			
				
				$query	= 'select * from twse_daily_trading_info WHERE corp_id="'. $corp_id . '" AND date="' . $date . '"'; 
				$result	= $db->sql_query_limit($query, 1); 
				if ($r = $db->sql_fetchrow($result))
				{						
					$closing_price[] = $r['closing_price'];
				}	
				$db->sql_freeresult($result);

			} //foreach ($day_set as $d)		
		} //foreach ($month_set as $m)
	} //foreach ($year_set as $y)	
	
	
	$get_closing_price = time() - $s;
	$s = time();
	
	// generate peaks/valleys.
	$window		= 360;				// to get the highest and lowest price within the window.
	$step		= (int)($window *0.6);
	$fuzzy		= (int)$window/10;		// checking the 20 days before.
	$tolerance 	= 0.1;		// if the difference is within the tolerance range, include it. 
	
	$peak_A 	= gen_peak($closing_price, $window, $step, $fuzzy, $tolerance);
	
	$window		= 360;				// to get the highest and lowest price within the window.
	$step		= (int)($window *0.3);
	$fuzzy		= (int)$window/10;		// checking the 20 days before.
	$tolerance 	= 0.1;		// if the difference is within the tolerance range, include it. 
	
	$peak_B 	= gen_peak($closing_price, $window, $step, $fuzzy, $tolerance);
	
	$window		= 270;				// to get the highest and lowest price within the window.
	$step		= (int)($window *0.6);
	$fuzzy		= (int)$window/10;		// checking the 20 days before.
	$tolerance 	= 0.1;		// if the difference is within the tolerance range, include it. 
	
	$peak_C 	= gen_peak($closing_price, $window, $step, $fuzzy, $tolerance);
	
	$window		= 270;				// to get the highest and lowest price within the window.
	$step		= (int)($window *0.3);
	$fuzzy		= (int)$window/10;		// checking the 20 days before.
	$tolerance 	= 0.1;		// if the difference is within the tolerance range, include it. 
	
	$peak_D 	= gen_peak($closing_price, $window, $step, $fuzzy, $tolerance);
	
	$window		= 180;				// to get the highest and lowest price within the window.
	$step		= (int)($window *0.6);
	$fuzzy		= (int)$window/10;		// checking the 20 days before.
	$tolerance 	= 0.1;		// if the difference is within the tolerance range, include it. 
	
	$peak_E 	= gen_peak($closing_price, $window, $step, $fuzzy, $tolerance);
	
	$window		= 180;				// to get the highest and lowest price within the window.
	$step		= (int)($window *0.3);
	$fuzzy		= (int)$window/10;		// checking the 20 days before.
	$tolerance 	= 0.1;		// if the difference is within the tolerance range, include it. 
	
	$peak_F 	= gen_peak($closing_price, $window, $step, $fuzzy, $tolerance);
	
	$gen_peak_time = time() - $s;
	$s = time();
	
	// don't go over today.
	$today = get_date_from_time(get_taiwan_time());	
	
	$i = 0;
	// Retrieve data from database and write out report.  
	foreach ($year_set as $y)
	{
		foreach ($month_set as $m)
		{
			foreach ($day_set as $d)
			{
				$date = $y . $m . $d;			
				
				// don't go over today.
				if (strcmp($date, $today) >0 )
					break;
				
				// get index volume (only the days with index and volume)				
				$query	= 'select * from twse_daily_trading_info WHERE corp_id="'. $corp_id . '" AND date="' . $date . '"'; 
				$result	= $db->sql_query_limit($query, 1); 
				if ($r = $db->sql_fetchrow($result))
				{
					$str = ' ' . $date . ', ' . $r['trade_volume'] . ', ' . $r['trade_value'] . ', ' . $r['opening_price'] . ', ' . $r['highest_price'] . ', ' . $r['lowest_price'] . ', ' . $r['closing_price'] . ', ' . $r['change'] . ', ' . $r['transaction'] . ', ';
					
					// get TAIEX
					// get index volume (only the days with index and volume)
					$sql 	= 'SELECT * FROM taiex_index_volume WHERE date="' . $date . '"';
					$result = $db->sql_query_limit($sql, 1); 
					if ($r = $db->sql_fetchrow($result))
					{			
						$str .=	$r['closing_index'] . ', ' .  $r['volume'] . ', ';							
					}
					else
					{
						$str .= ' , , ';
					}
					$db->sql_freeresult($result);
					
					// calculate PMA
					$ma5	= pre_ma($closing_price, 5, $i);
					$ma10	= pre_ma($closing_price, 10, $i);
					$ma20	= pre_ma($closing_price, 20, $i);
					$ma30	= pre_ma($closing_price, 30, $i);
					$ma60	= pre_ma($closing_price, 60, $i);
					$ma120	= pre_ma($closing_price, 120, $i);
					
					// calculate RSI
					$RSI20	= RSI($closing_price, 20, $i);
					$RSI40	= RSI($closing_price, 40, $i);
					
					
					$str .= '' . "$ma5, $ma10, $ma20, $ma30, $ma60, $ma120, $RSI20, $RSI40, " . $peak_A[$i] . ', ' . $peak_B[$i] . ', ' . $peak_C[$i] . ', ' . $peak_D[$i] . ', ' . $peak_E[$i] . ', ' . $peak_F[$i] . ', ';
										
					++$i;							
					
					$str .= "\n";
					//echo "\n";				
					
					if ($local_report)
						fwrite($report, $str);
					else
						echo $str;
				}
									
				$db->sql_freeresult($result);	
			} //foreach ($day_set as $d)		
		} //foreach ($month_set as $m)
	} //foreach ($year_set as $y)	
	
	
	$gen_report_time = time() - $s;		
	
	if ($debug)
	{
//		echo "get_closing_price:$get_closing_price <BR>\n";
//		echo "gen_peak_time:$gen_peak_time <BR>\n";
//		echo "gen_report_time:$gen_report_time <BR>\n";
	}
	
	if ($local_report)
	{
		fclose($report);
		$corps_in_local_reports[] = $corp_id;
	}
}


function gen_report($corp_id, $local_report = false)
{
	global $db, $debug;
	global $year_set, $month_set, $day_set;
	global $local_report_path, $corps_in_local_reports;
		
	$report	= false;
	
	$str = ' ' . $corp_id . '-' . get_corp_chinese_name($corp_id) . '-date, trade_volume, trade_value, opening_price, highest_price, lowest_price, closing_price, change, transaction, TAIEX, TAIEX_volume, ma5, ma10, ma20, ma30, ma60, ma120, RSI20, RSI40, peak_1_A, peak_1_B, peak_1_C, peak_1_D, peak_1_E, peak_1_F' . "\n";	
	
	if ($local_report)
	{
		// check if the corp has any record and if the corp has enough data to analyse, or SPSS will fail
		// a basic threshold is 5 years => 5*250 = 750 trading days 		
		$query	= 'SELECT COUNT(DISTINCT date) AS num FROM twse_daily_trading_info WHERE corp_id="'. $corp_id . '" '; 
		$result	= $db->sql_query_limit($query, 1); 
		$row 	= $db->sql_fetchrow($result);
		$num	= $row['num'];
		
		if ($row == false || $num < 750)
		{					
			if ($debug)
				echo "no record for $corp_id, or too few records for the corp:$num <BR>\n";							
			return;
		}	
		$db->sql_freeresult($result);
		
		
		// open file to write 
		$file_path 	= $local_report_path . $corp_id . '.csv';
		$report 	= fopen($file_path, 'w');
		if ($report == false)
			return;
			
		fwrite($report, $str);		
	}
	else
	{
		// print csv header.
		echo $str;
	}
	
	$s = microtime(true);
	
	// Get all information  before calculating peaks and valleys  	
	$date				= array();
	$trade_volume		= array();
	$trade_value		= array();
	$opening_price		= array();
	$highest_price		= array();
	$lowest_price		= array();
	$closing_price		= array();
	$change				= array();
	$transaction		= array();
	$TAIEX_date			= array();
	$TAIEX_index		= array();
	$TAIEX_volume		= array();	
	$initial_date		= '';
	
	// get stock trading data
	$query	= 'select * from twse_daily_trading_info WHERE corp_id="'. $corp_id . '" ORDER BY date '; 
	$result	= $db->sql_query($query); 
	while ($r = $db->sql_fetchrow($result))
	{
		if ($initial_date == '')
			$initial_date = $r['date'];
		
		$date[] 			= $r['date'];
		$trade_volume[] 	= $r['trade_volume'];
		$trade_value[] 		= $r['trade_value'];
		$opening_price[] 	= $r['opening_price'];
		$highest_price[] 	= $r['highest_price'];
		$lowest_price[] 	= $r['lowest_price'];
		$closing_price[] 	= $r['closing_price'];
		$change[] 			= $r['change'];
		$transaction[] 		= $r['transaction'];	
		
	}
	
	$db->sql_freeresult($result);
	
	// get TAIEX index volume (only the days with index and volume)
	$sql 	= 'SELECT * FROM taiex_index_volume WHERE date>="' . $initial_date . '" ORDER BY date ';
	$result = $db->sql_query($sql); 
	while ($r = $db->sql_fetchrow($result))
	{
		$TAIEX_date[] 		= $r['date'];
		$TAIEX_index[] 		= $r['closing_index'];
		$TAIEX_volume[] 	= $r['volume'];
	}

	$db->sql_freeresult($result);
		
	$get_closing_price = microtime(true) - $s;
	$s = microtime(true);
	
	global $model_parameter;
	
	// generate peaks/valleys.	
	$peak_A 	= gen_peak($closing_price, $model_parameter['A']['window'], $model_parameter['A']['step'], 
							$model_parameter['A']['fuzzy'], $model_parameter['A']['tolerance']);
	
	$peak_B 	= gen_peak($closing_price, $model_parameter['B']['window'], $model_parameter['B']['step'], 
							$model_parameter['B']['fuzzy'], $model_parameter['B']['tolerance']);
							
	$peak_C 	= gen_peak($closing_price, $model_parameter['C']['window'], $model_parameter['C']['step'], 
							$model_parameter['C']['fuzzy'], $model_parameter['C']['tolerance']);							
	
	$peak_D 	= gen_peak($closing_price, $model_parameter['D']['window'], $model_parameter['D']['step'], 
							$model_parameter['D']['fuzzy'], $model_parameter['D']['tolerance']);
							
	$peak_E 	= gen_peak($closing_price, $model_parameter['E']['window'], $model_parameter['E']['step'], 
							$model_parameter['E']['fuzzy'], $model_parameter['E']['tolerance']);							

	$peak_F 	= gen_peak($closing_price, $model_parameter['F']['window'], $model_parameter['F']['step'], 
							$model_parameter['F']['fuzzy'], $model_parameter['F']['tolerance']);
		
	/*
	$window		= 60;				// to get the highest and lowest price within the window.
	$step		= (int)($window *0.05);
	$fuzzy		= (int)$window/10;		// checking the 20 days before.
	$tolerance 	= 0.1;		// if the difference is within the tolerance range, include it. 
	
	$peak_A 	= gen_peak($closing_price, $window, $step, $fuzzy, $tolerance);
	
	$window		= 60;				// to get the highest and lowest price within the window.
	$step		= (int)($window *0.15);
	$fuzzy		= (int)$window/10;		// checking the 20 days before.
	$tolerance 	= 0.1;		// if the difference is within the tolerance range, include it. 
	
	$peak_B 	= gen_peak($closing_price, $window, $step, $fuzzy, $tolerance);
	
	$window		= 90;				// to get the highest and lowest price within the window.
	$step		= (int)($window *0.05);
	$fuzzy		= (int)$window/10;		// checking the 20 days before.
	$tolerance 	= 0.1;		// if the difference is within the tolerance range, include it. 
	
	$peak_C 	= gen_peak($closing_price, $window, $step, $fuzzy, $tolerance);
	
	$window		= 90;				// to get the highest and lowest price within the window.
	$step		= (int)($window *0.15);
	$fuzzy		= (int)$window/10;		// checking the 20 days before.
	$tolerance 	= 0.1;		// if the difference is within the tolerance range, include it. 
	
	$peak_D 	= gen_peak($closing_price, $window, $step, $fuzzy, $tolerance);
	
	$window		= 180;				// to get the highest and lowest price within the window.
	$step		= (int)($window *0.05);
	$fuzzy		= (int)$window/10;		// checking the 20 days before.
	$tolerance 	= 0.1;		// if the difference is within the tolerance range, include it. 
	
	$peak_E 	= gen_peak($closing_price, $window, $step, $fuzzy, $tolerance);
	
	$window		= 180;				// to get the highest and lowest price within the window.
	$step		= (int)($window *0.15);
	$fuzzy		= (int)$window/10;		// checking the 20 days before.
	$tolerance 	= 0.1;		// if the difference is within the tolerance range, include it. 
	
	$peak_F 	= gen_peak($closing_price, $window, $step, $fuzzy, $tolerance);
	*/
	
	$gen_peak_time = microtime(true) - $s;
	$s = microtime(true);
	
	$i = $j = 0;
	
	while($i < count($date) && $j < count($TAIEX_date))
	{
		if ($date[$i] != $TAIEX_date[$j])
		{
			//if ($debug)
			//	echo "i:$i j:$j error, date not the same " . $date[$i] . ' => TAIEX date ' . $TAIEX_date[$j] . '<BR>' . PHP_EOL;	
				
			if ($date[$i] > $TAIEX_date[$j])
			{
				++ $j;
				continue;
			}
			else
			{
				++ $i;
				continue;
			}			
		}
		
		$str = ' ' . $date[$i] . ', ' . $trade_volume[$i] . ', ' . $trade_value[$i] . ', ' . $opening_price[$i] . ', ' . $highest_price[$i] . ', ' . $lowest_price[$i] . ', ' . $closing_price[$i] . ', ' . $change[$i] . ', ' . $transaction[$i] . ', ';
		$str .=	' ' . $TAIEX_index[$j] . ', ' .  $TAIEX_volume[$j] . ', ';							
	
		// calculate PMA
		$ma5	= pre_ma($closing_price, 5, $i);
		$ma10	= pre_ma($closing_price, 10, $i);
		$ma20	= pre_ma($closing_price, 20, $i);
		$ma30	= pre_ma($closing_price, 30, $i);
		$ma60	= pre_ma($closing_price, 60, $i);
		$ma120	= pre_ma($closing_price, 120, $i);
					
		// calculate RSI
		$RSI20	= RSI($closing_price, 20, $i);
		$RSI40	= RSI($closing_price, 40, $i);		
					
		$str .= '' . "$ma5, $ma10, $ma20, $ma30, $ma60, $ma120, $RSI20, $RSI40, " . $peak_A[$i] . ', ' . $peak_B[$i] . ', ' . $peak_C[$i] . ', ' . $peak_D[$i] . ', ' . $peak_E[$i] . ', ' . $peak_F[$i] . PHP_EOL;
					
		if ($local_report)
			fwrite($report, $str);
		else
			echo $str;
					
		++ $i;	
		++ $j;
	}
	
	
	$gen_report_time = microtime(true) - $s;		
	
	if ($debug)
	{
		echo "get_closing_price:$get_closing_price <BR>\n";
		echo "gen_peak_time:$gen_peak_time <BR>\n";
		echo "gen_report_time:$gen_report_time <BR>\n";
	}
	
	if ($local_report)
	{
		fclose($report);
		$corps_in_local_reports[] = $corp_id;
	}	
	
}

function gen_SPSS_syntax($local_report_path)
{
	global $corps_in_local_reports, $syntax_body;
	
	// create result folder 
	$result_folder_path = $local_report_path . 'Result\\';
	mkdir($result_folder_path);
	
	$path = $result_folder_path . 'prophet.sps';
	
	$syntax 	= fopen($path, 'w');
	if ($syntax == false)
		return;

	fwrite($syntax, $syntax_body);

	foreach($corps_in_local_reports as $corp_id)
	{
		$csv_path 		= $local_report_path . $corp_id . '.csv';	
		$result_path 	= $result_folder_path . $corp_id . '.csv';	
		
		$str = '!AnalyseStock path="' . $csv_path . '" savepath="' . $result_path . '".' . PHP_EOL;
		fwrite($syntax, $str);
	}
		
	fclose($syntax);
	
	return $path;
}

function get_international_index($date, $table_name)
{
	global $db, $debug;
	
	$sql 	= 'SELECT * FROM ' . $table_name . ' WHERE Date="' . $date . '"';
	$result = $db->sql_query_limit($sql, 1); 
	if ($r = $db->sql_fetchrow($result))
	{
		//print_r($r);
		return $r;
	}
	
	return false;	
}

function print_index($p)
{
	echo $p['Date'] . ', ';							
//	echo $p['Open'] . ', ';
//	echo $p['High'] . ', ';							
//	echo $p['Low'] . ', ';
	echo $p['Close'] . ', ';						
//	echo $p['Volume'] . ', ';
//	echo $p['Adj Close'] . ', ';								
}


function RSI($taiex, $width, $idx)
{
	// echo "RSI: width:$width, idx:$idx\n";
	
	// if unexpected error
	if (sizeof($taiex) <= 0 || $idx > sizeof($taiex) || $width <= 0 || sizeof($taiex) <= $width)
		return 0;
		
	if ($idx <= $width)
		return 50;
		
	$r = get_AU_AD($taiex, $width, $idx);
	if (sizeof($r) != 2)
		return 50;		// return a middle value in case of errors	
	
	$AU = $r['AU'];
	$AD = $r['AD'] + 1;	// add 1 to avoide divided by zero case
		
	$RS = abs($AU/$AD);
		
	$RSI = 100 - (100/(1 + $RS));
	
	return $RSI;
}

function get_AU_AD($taiex, $width, $idx)
{
	// if unexpected error
	if (sizeof($taiex) <= 0 || $idx > sizeof($taiex) || $width <= 0 || sizeof($taiex) <= $width)
		return 0;	
			
	$i 		= $idx - $width;
	$end 	= $idx;
	$AU		= 0;
	$AD		= 0;
	
	while ($i < $end)
	{
		if ($taiex[$i] < $taiex[$i+1])
		{
			$AU += ($taiex[$i+1] - $taiex[$i]);	
		}		
		else if ($taiex[$i] > $taiex[$i+1])
		{
			$AD += ($taiex[$i] - $taiex[$i+1]);	
		}	
		
		$i += 1;	
	}
	
	$r['AU'] = $AU;
	$r['AD'] = $AD;
	
	return $r;
}


function pre_ma($taiex, $width, $idx)
{
	//echo "pre_ma: width:$width, idx:$idx\n";
	
	if (sizeof($taiex) <= 0 || $idx > sizeof($taiex) || $width <= 0)
		return 0;		
	if (sizeof($taiex) <= $width)
		return sum($taiex)/ sizeof($taiex);
			
	if ($idx < $width)
		return sum($taiex, 0, $idx) / ($idx + 1);
		
	return sum($taiex, $idx - $width + 1, $idx) / $width;	
}

function center_ma($taiex, $width, $idx)
{
	//echo "center_ma: width:$width, idx:$idx\n";
	
	if (sizeof($taiex) <= 0 || $idx > sizeof($taiex) || $width <= 0)
		return 0;		
	if (sizeof($taiex) <= $width)
		return sum($taiex)/ sizeof($taiex);
				
	if ($idx < ($width / 2))
		return sum($taiex, 0, $idx + ($width / 2)) / ($idx + ($width/2) + 1);
		
	if ($idx + ($width / 2) > sizeof($taiex))
		return sum($taiex, $idx - ($width / 2), sizeof($taiex)) / (sizeof($taiex) - $idx + ($width/2));
		
	return sum($taiex, $idx - ($width/2), $idx + ($width/2)) / (($width/2)*2 + 1);	
}

function sum($taiex, $start = 0, $end = -1)
{
	if ($end == -1)
		$end = sizeof($taiex) - 1;
		
	if ($start > $end || sizeof($taiex) == 0 || sizeof($taiex) < $end)
		return 0;
		
	$i = $start;
	$s = 0;
	
	while ($i <= $end)
	{
		$s = $s + $taiex[$i];
		++ $i;
	}
	
	return $s;
}

function get_max($taiex, $i, $window)
{
	$k 		= $i + 1;
	$j		= $i;
	$max	= $taiex[$j];
	
	while ($k < $i + $window)
	{
		if ($taiex[$k] > $max)
		{
			$max = $taiex[$k];
			$j = $k;	
		}
	
		++$k;	
	}
	
	return $j;
}

function get_min($taiex, $i, $window)
{
	$k 		= $i + 1;
	$j		= $i;
	$min		= $taiex[$j];
	
	while ($k < $i + $window)
	{
		if ($taiex[$k] < $min)
		{
			$min = $taiex[$k];
			$j = $k;	
		}
	
		++$k;	
	}
	
	return $j;
}

get_TWSE_data();

get_international_index_from_Yahoo();

gen_report();





function get_international_index_from_Yahoo()
{
	global $db, $debug;
	global $year_set, $month_set, $day_set;

	if ($debug)
		echo "get_international_index_from_Yahoo - begin\n";
			
	get_index_from_Yahoo('index_cac40', 	'%5EFCHI');
//	get_index_from_Yahoo('index_dji', 		'%5EDJI');
	get_index_from_Yahoo('index_ftse100', 	'%5EFTSE');
	get_index_from_Yahoo('index_hangseng', 	'%5EHSI');
	get_index_from_Yahoo('index_nasdaq', 	'%5EIXIC');
	get_index_from_Yahoo('index_shanghai', 	'000001.SS');
	get_index_from_Yahoo('index_sp500', 	'%5EGSPC');
	get_index_from_Yahoo('index_gdaxi', 	'%5EGDAXI');
	get_index_from_Yahoo('index_nikkei', 	'%5EN225');
	
	get_index_from_Yahoo('interest_rate_10_year_cboe', 	'%5ETNX', true);
	
	
	return;
}

// get figures from Yahoo! Finance. For interest rates, $volume_can_be_zero should be set to true since the volume is always zero.
function get_index_from_Yahoo($table_name, $symbol_in_yahoo, $volume_can_be_zero = false)
{
	global $db, $debug;
	
	if ($debug)
		echo "get_index_from_Yahoo, $table_name, $symbol_in_yahoo  - begin\n";
		
	$today = get_date_from_time(get_taiwan_time());	
	if ($debug)
		echo "today: $today\n";
		
	$last_date	= '';
	$query 		= 'SELECT * from ' . $table_name . ' ORDER BY date DESC';
	$result		= $db->sql_query_limit($query, 1);
	if ($row = $db->sql_fetchrow($result))
	{
		$last_date = $row['Date'];
		if ($debug)
			echo "last_date: $last_date\n";
	}
	
	if (strcmp($last_date, $today) >= 0 )
		return;
		
	
		// query Yahoo! Finance		
		$from_y = get_year_from_date($last_date);
		$from_m = (int)(get_month_from_date($last_date)) - 1;
		$from_d = get_day_from_date($last_date);
		$to_y	= get_year_from_date($today);
		$to_m	= (int)(get_month_from_date($today)) - 1;
		$to_d	= get_day_from_date($today);
		
		$url = "http://ichart.finance.yahoo.com/table.csv?s=$symbol_in_yahoo&a=$from_m&b=$from_d&c=$from_y&d=$to_m&e=$to_d&f=$to_y&g=d&ignore=.csv";
		if ($debug)
			echo "$url \n";
		
		$raw_file = file_get_contents($url);
    	if ($raw_file == false)
    		return '';
	
    	$csv 		= sv_str_getcsv ($raw_file, "\n");    
    	//print_r($csv);
    
    	foreach($csv as &$row)
    	{
	    	$row = sv_str_getcsv($row[0]);
	    	if (sizeof($row[0]) != 7)
	    		continue;
    
	    	if ($debug)
	    		print_r($row[0]);	  
	    	
	    	$q_date 	= $row[0][0];
	    	$q_open 	= $row[0][1];
	    	$q_high 	= $row[0][2];
	    	$q_low 		= $row[0][3];
	    	$q_close 	= $row[0][4];
	    	$q_volume 	= $row[0][5];
	    	$q_adj_close= $row[0][6];
	    
	    	if ($q_date == '' || strlen($q_date) != 10)
	    		continue;	    		
	    	if ($volume_can_be_zero == false && $q_volume == 0)
	    		continue;
	    	
	    	$q_date = get_date_from_Yahoo_date($q_date);
	    		
	    	if ($debug)
	    		echo "$q_date\n";
	    		
	    	// check if the record already exist	
	    	$query 		= 'SELECT * from  ' . $table_name . '  WHERE date="' . $q_date . '"';
			$result		= $db->sql_query_limit($query, 1);
			
			if ($debug)
	    		echo "$query\n";
	    		
			if ($row = $db->sql_fetchrow($result))
			{
				if ($debug)
					echo "the record for $q_date already exist \n";
					
				continue;
			}
		
			// add new records.
			$sql = 'INSERT INTO  ' . $table_name . '  (Date, Open, High, Low, Close, Volume, `Adj Close`) VALUES ("' . $q_date . '" ,' .  $q_open . ' ,' . $q_high . ' ,' . $q_low . ' ,' . $q_close . ' ,' . $q_volume . ' ,' . $q_adj_close .	')';
    		if ($debug)
	    		echo "$sql\n";
			$db->sql_query($sql);			
    	}
	
}

// get date (format: YYYYMMDD) from Yahoo date (format: YYYY-MM-DD)
function get_date_from_Yahoo_date($yahoo_date)
{
	if (strlen($yahoo_date) != 10)
		return '';
		
	$y = substr($yahoo_date, 0, 4);
	$m = substr($yahoo_date, 5, 2);
	$d = substr($yahoo_date, 8, 2);
	
	return $y.$m.$d;
}

function get_TWSE_data($only_add_new_record = true)
{
	global $db, $debug;
	global $year_set, $month_set, $day_set;

	if ($debug)
		echo "get_TWSE_data - begin\n";
	
	// don't go over today.
	$today = get_date_from_time(get_taiwan_time());	
	if ($debug)
		echo "today: $today\n";
			
	$last_date	= '';
	$query 		= 'SELECT * from taiex_index_volume ORDER BY date DESC';
	$result		= $db->sql_query_limit($query, 1);
	if ($row = $db->sql_fetchrow($result))
	{
		$last_date = $row['date'];
		if ($debug)
				echo "last_date: $last_date\n";
	}
	
	foreach ($year_set as $y)
	{
		foreach ($month_set as $m)
		{
			foreach ($day_set as $d)
			{	
				$date = $y . $m . $d;				
				
				if ($only_add_new_record)
				{
					// skip old records.
					if (strcmp($date, $last_date) <=0 )
						continue;						
				}
				
				// don't go over today.
				if (strcmp($date, $today) >0 )
						break;						

				get_index_volume($y, $m, $d);				
			}
		}		
	}
	
	return;
	
		
	$last_date	= '';
	$query 		= 'SELECT * from margin_transaction_summary ORDER BY date DESC';
	$result		= $db->sql_query_limit($query, 1);
	if ($row = $db->sql_fetchrow($result))
	{
		$last_date = $row['date'];
		if ($debug)
			echo "margin_transaction_summary - last_date: $last_date\n";
	}
	
	foreach ($year_set as $y)
	{
		foreach ($month_set as $m)
		{
			foreach ($day_set as $d)
			{	
				$date = $y . $m . $d;				
				
				if ($only_add_new_record)
				{
					// skip old records.
					if (strcmp($date, $last_date) <=0 )
						continue;						
				}
				
				// don't go over today.
				if (strcmp($date, $today) >0 )
						break;						

				get_short_marginal_sale($y, $m, $d);
			}
		}		
	}
		
	return;
}


// Get the TAIEX index of a specific date. 
function get_index_volume($y, $m, $d)
{
	global $db, $debug;
	
	$date = $y . $m . $d;			
	
	// search from database.
	$query	= "select * from taiex_index_volume where date=$date"; 
	$result	= $db->sql_query_limit($query, 1); 
	if( ($row = $db->sql_fetchrow($result)))
	{
//		echo "index at $date alrady exist\n";
		return ;
	}	
	
	
	$url = "http://www.twse.com.tw/ch/trading/exchange/FMTQIK/FMTQIK2.php?STK_NO=&myear=$y&mmon=$m&type=csv";
	//echo $url;
	
    $raw_file = file_get_contents($url);
    if ($raw_file == false)
    	return '';
	
    $csv 		= sv_str_getcsv ($raw_file, "\n");    
    //print_r($csv);
    
    foreach($csv as &$row)
    {
	    $row = sv_str_getcsv($row[0]);
	    if (sizeof($row[0]) != 6)
	    	continue;
    
	    //print_r($row[0]);
	    	     
	    $q_date 	= $row[0][0];
	    $q_volume 	= $row[0][2];
	    $q_index 	= $row[0][4];
	    
	    if ($q_date == '')
	    	continue;
	    	 
//	    echo "$q_date, $q_volume, $q_index\n";	    	       
	    
		$y = substr($q_date, 0, strlen($q_date) -6);
		$m = substr($q_date, strlen($q_date) - 5, 2);
		$d = substr($q_date, strlen($q_date) - 2, 2);
    		
    	$y = (int)($y) + 1911;
    	$i_date = '' . $y . $m . $d;
    		
    	$query	= "select * from taiex_index_volume where date=$i_date"; 
		$result	= $db->sql_query_limit($query, 1); 
		if( ($row = $db->sql_fetchrow($result)))
		{
//			echo "index/volume at $i_date alrady exist\n";
			continue;
		}	 
		
    	$q_index = ereg_replace("[^0-9.]", "", $q_index); 			
    	$i_closing_index = (float)($q_index);
    		
    	$i_volume = ereg_replace("[^0-9]", "", $q_volume);
    	
    	// from 20080701, volume base is changed from 1000 to 1.
    	if (strcmp($i_date, '20080701' ) < 0)
    		$i_volume = $i_volume * 1000;
    	    	
    	// insert the index record into the database.
    	$sql = 'INSERT IGNORE INTO taiex_index_volume SET date="' . $i_date . '", closing_index=' . $i_closing_index . ', volume=' . $i_volume;
		if ($debug)
    	    	echo "    $sql\n";
    	$result	= $db->sql_query($sql);    		
	}
	
    return '';		
}

function gen_ann_report($corp_id)
{
	global $db, $debug;
	global $year_set, $month_set, $day_set;
	
	// print csv header.
	//echo ' ' . $corp_id . '-' . get_corp_chinese_name($corp_id) . '-date, trade_volume, trade_value, opening_price, highest_price, lowest_price, closing_price, change, transaction, TAIEX, TAIEX_volume, peak_1_A, peak_1_B, peak_1_C, peak_1_D, peak_1_E, peak_1_F';
	//echo "\n";	
	$debug = true;
	
	$n = new NeuralNetwork(11, 12, 1);
	$n->setVerbose(false);

	// Get all closing_price before calculating peaks and valleys  
	$closing_price		= array();
	
	$t1 = time();
	
	foreach ($year_set as $y)
	{
		foreach ($month_set as $m)
		{
			foreach ($day_set as $d)
			{
				// get stock price
				$date = $y . $m . $d;			
				
				$query	= 'select closing_price from twse_daily_trading_info WHERE corp_id="'. $corp_id . '" AND date="' . $date . '"'; 
				$result	= $db->sql_query_limit($query, 1); 
				if ($r = $db->sql_fetchrow($result))
				{
					$closing_price[] = $r['closing_price'];
				}	
				
				$db->sql_freeresult($result);

			} //foreach ($day_set as $d)		
		} //foreach ($month_set as $m)
	} //foreach ($year_set as $y)	
	
	$t1 = time() - $t1;
	echo "reading closing_price from DB takes $t1 seconds.\n";
	
	
	
	$t1 = time();
	
	// generate peaks/valleys.
	$window		= 360;				// to get the highest and lowest price within the window.
	$step		= (int)($window *0.8);
	$fuzzy		= (int)$window/10;		// checking the 20 days before.
	$tolerance 	= 0.1;		// if the difference is within the tolerance range, include it. 
	
	$peak_A 	= gen_peak($closing_price, $window, $step, $fuzzy, $tolerance);
	
	$t1 = time() - $t1;
	echo "generating peaks takes $t1 seconds.\n";
	
	// don't go over today.
	$today = get_date_from_time(get_taiwan_time());	

	// Get the training data(80% of the total data)
	$i = 0;
	$t1 = time();
	// Retrieve data from database and write out report.  
	foreach ($year_set as $y)
	{
		foreach ($month_set as $m)
		{
			foreach ($day_set as $d)
			{
				$date = $y . $m . $d;			
				
				// don't go over today.
				if (strcmp($date, $today) >0 )
					break;
				
				if (i > sizeof($closing_price) * 0.8 )
					break;				
				
				// get index volume (only the days with index and volume)				
				$query	= 'select * from twse_daily_trading_info WHERE corp_id="'. $corp_id . '" AND date="' . $date . '"'; 
				$result	= $db->sql_query_limit($query, 1); 
				if ($r = $db->sql_fetchrow($result))
				{												
					$v_1 = $r['trade_volume'];
					$v_2 = $r['trade_value'];
					$v_3 = $r['opening_price'];
					$v_4 = $r['highest_price'];
					$v_5 = $r['lowest_price'];
					$v_6 = $r['closing_price'];
					$v_7 = $r['change'];
					$v_8 = $r['transaction'];												
					
					// get TAIEX
					// get index volume (only the days with index and volume)
					$sql 	= 'SELECT * FROM taiex_index_volume WHERE date="' . $date . '"';
					$result = $db->sql_query_limit($sql, 1); 
					if ($r = $db->sql_fetchrow($result))
					{					
						$v_9 = $r['closing_index'];
						$v_10= $r['volume'];
					}
					
					$n->addTestData(array ($v_1,$v_2,$v_3,$v_4,$v_5,$v_6,$v_7,$v_8,$v_9,$v_10, 1 ), array ($peak_A[$i]));
										
					
					++$i;				
				}
									
					
			} //foreach ($day_set as $d)		
		} //foreach ($month_set as $m)
	} //foreach ($year_set as $y)	
	
	
	$t1 = time() - $t1;
	echo "reading from DB takes $t1 seconds.\n";
	
	// we try training the network for at most $max times
	$max = 3;
	$t1 = time();
	// train the network in max 1000 epochs, with a max squared error of 0.01
	while (!($success = $n->train(1000, 0.01)) && $max -- > 0) {
		echo "Nothing found...<hr />";
	}
	
	// print a message if the network was succesfully trained
	if ($success) {
	    $epochs = $n->getEpoch();
		echo "Success in $epochs training rounds!<hr />";
	}

	$t1 = time() - $t1;
	echo "training takes $t1 seconds.\n";
	
	echo sizeof($closing_price);	
	echo "start to apply to last 20%\n";
	
	
	$i = 0;
	// Retrieve data from database and write out report.  
	foreach ($year_set as $y)
	{
		foreach ($month_set as $m)
		{
			foreach ($day_set as $d)
			{
				$date = $y . $m . $d;			
				//echo "testing: date:$date today:$today\n";
				
				// don't go over today.
				if (strcmp($date, $today) >0 )
				{
					//echo "don't go over today.: date:$date today:$today\n";
					break;
				}
				
				
								
				
				// get index volume (only the days with index and volume)				
				$query	= 'select * from twse_daily_trading_info WHERE corp_id="'. $corp_id . '" AND date="' . $date . '"'; 				
				//echo "$query\n";				
				$result	= $db->sql_query_limit($query, 1); 
				if ($r = $db->sql_fetchrow($result))
				{
					++$i;
				
					// apply to the last 20%
//					if ($i < sizeof($closing_price) * 0.8 )
//						continue;				
					
					//echo "testing: get stock trading data from db \n";
					
					echo $date . ', ';
							
					echo $r['trade_volume'] . ', ';
					echo $r['trade_value'] . ', ';
					echo $r['opening_price'] . ', ';
					echo $r['highest_price'] . ', ';
					echo $r['lowest_price'] . ', ';
					echo $r['closing_price'] . ', ';
					echo $r['change'] . ', ';
					echo $r['transaction'] . ', ';					

					$v_1 = $r['trade_volume'];
					$v_2 = $r['trade_value'];
					$v_3 = $r['opening_price'];
					$v_4 = $r['highest_price'];
					$v_5 = $r['lowest_price'];
					$v_6 = $r['closing_price'];
					$v_7 = $r['change'];
					$v_8 = $r['transaction'];					
									
					
					// get TAIEX
					// get index volume (only the days with index and volume)
					$sql 	= 'SELECT * FROM taiex_index_volume WHERE date="' . $date . '"';
					$result = $db->sql_query_limit($sql, 1); 
					if ($r = $db->sql_fetchrow($result))
					{					
						echo $r['closing_index'] . ', ';
						echo $r['volume'] . ', ';
						
						$v_9 = $r['closing_index'];
						$v_10= $r['volume'];
					}
					else
					{
						echo ' , , ';	
					}
					
					echo $peak_A[$i] . ', ';	
					
					//echo "testing: calling calculate()\n";
					$output = $n->calculate( array ($v_1,$v_2,$v_3,$v_4,$v_5,$v_6,$v_7,$v_8,$v_9,$v_10, 1 ));									
					$o = (int)($output[0]);
					echo $o . ', ';	
					
										
					echo "\n";				

				}
				else
				{
					//echo "fail to get data from db\n";				
				}
									
				
			} //foreach ($day_set as $d)		
		} //foreach ($month_set as $m)
	} //foreach ($year_set as $y)	
	
	//echo "end\n";
	
}


?>